package tree.traverse;

/**
 * @ author : HyoJoo-W
 * @ createDate : 2021-06-04 16:28
 * @ Description :
 */
public class TreeNode
{
    private int no;
    private String name;

    private TreeNode left;
    private TreeNode right;

    public TreeNode(int no, String name)
    {
        this.no = no;
        this.name = name;
    }

    public int getNo()
    {
        return no;
    }

    public void setNo(int no)
    {
        this.no = no;
    }

    public String getName()
    {
        return name;
    }

    public void setName(String name)
    {
        this.name = name;
    }

    public TreeNode getLeft()
    {
        return left;
    }

    public void setLeft(TreeNode left)
    {
        this.left = left;
    }

    public TreeNode getRight()
    {
        return right;
    }

    public void setRight(TreeNode right)
    {
        this.right = right;
    }

    @Override
    public String toString()
    {
        return "TreeNode{" +
                "no=" + no +
                ", name='" + name + '\'' +
                '}';
    }

    public void preOrder()
    {
        //1. 输出当前节点
        System.out.println(this);

        //2. 递归前序遍历左子树
        if (this.left != null)
        {
            this.left.preOrder();
        }

        //3. 递归前序遍历右子树
        if (this.right != null)
        {
            this.right.preOrder();
        }
    }

    public void midOrder()
    {
        //1. 递归中序遍历左子树
        if (this.left != null)
        {
            this.left.midOrder();
        }

        //2. 输出当前节点
        System.out.println(this);

        //3. 递归中序遍历右子树
        if (this.right != null)
        {
            this.right.midOrder();
        }
    }

    public void postOrder()
    {
        //1. 递归后序遍历左子树
        if (this.left != null)
        {
            this.left.postOrder();
        }

        //2. 递归后序遍历右子树
        if (this.right != null)
        {
            this.right.postOrder();
        }

        //3. 输出当前节点
        System.out.println(this);
    }

    public TreeNode preOrderSearch(int findNo)
    {
        //1. 比较当前节点是否是要查找的
        if (findNo == this.no)
        {
            return this;
        }
        //2. 左子树不为空则递归前序查找
        TreeNode resNode = null;
        if (this.left != null)
        {
            resNode = this.left.preOrderSearch(findNo);
        }
        if (resNode != null)
        {
            return resNode;
        }
        //3. 右子树不为空则递归前序查找
        if (this.right != null)
        {
            resNode = this.right.preOrderSearch(findNo);
        }
        return resNode;
    }


    public TreeNode midOrderSearch(int findNo)
    {
        //1. 左子树不为空则递归查找
        TreeNode resNode = null;
        if (this.left != null)
        {
            resNode = this.left.midOrderSearch(findNo);
        }
        if (resNode != null)
        {
            return resNode;
        }

        //2. 比较当前节点是否是要查找的
        if (findNo == this.no)
        {
            return this;
        }

        //3. 右子树不为空则递归查找
        if (this.right != null)
        {
            resNode = this.right.midOrderSearch(findNo);
        }
        return resNode;
    }

    public TreeNode postOrderSearch(int findNo)
    {
        //System.out.println("hello");
        //1. 左子树不为空则递归查找
        TreeNode resNode = null;
        if (this.left != null)
        {
            resNode = this.left.postOrderSearch(findNo);
        }
        if (resNode != null)
        {
            return resNode;
        }

        //2. 右子树不为空则递归查找
        if (this.right != null)
        {
            resNode = this.right.postOrderSearch(findNo);
        }
        if (resNode != null)
        {
            return resNode;
        }

        System.out.println("查找");
        //3. 比较当前节点是否是要查找的
        if (findNo == this.no)
        {
            return this;
        }

        return resNode;
    }
}
